﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using HA.MT3DMS.DomainLayer;

using HA.ModFlow.Controls;
using HA.Data;
namespace HA.ModFlow.Controls.MT3DMSWindows
{
    /// <summary>
    /// winBasicTransport.xaml 的交互逻辑
    /// </summary>
    public partial class winBasicTransport : Window
    {
        public winBasicTransport(Project proj)
        {
            InitializeComponent();
            project = proj;
            MT3DMSData mtData = proj.Mt3dmsData;
            this._mtData = mtData;
            this._pcBTN = new MTPCBTN(_mtData.BasicTransport);
            this.DataContext = this._pcBTN;
            this.btnConcentration.Content = "初始浓度(" + HA.ModFlow.Domain.DomainLayer.UnitConverter.GetConcentration(mtData.BasicTransport.ModelOption.Units) + ")";
            libSpecies.ItemsSource = this._pcBTN.Species;
            //事件
            btnStrPer.Click += new RoutedEventHandler(btnStrPer_Click);         
            btnOutCtrol.Click +=new RoutedEventHandler(btnOutCtrol_Click); 
            btnPackage.Click += new RoutedEventHandler(btnPackage_Click);
            btnDefSpe.Click +=new RoutedEventHandler(btnDefSpe_Click);
            btnUnit.Click +=new RoutedEventHandler(btnUnit_Click);
            btnHTOP.Click +=new RoutedEventHandler(btnHTOP_Click);
            btnICBUND.Click +=new RoutedEventHandler(btnICBUND_Click);
            btnPorosity.Click +=new RoutedEventHandler(btnPorosity_Click);
            btnConcentration.Click += new RoutedEventHandler(btnConcentration_Click);
            pcPages = new MTPCPackages(_mtData);

            this.txtTitle1.Text = this._mtData.BasicTransport.Title1;
            this.txtTitle2.Text = this._mtData.BasicTransport.Title2;
        }

        #region  字段与属性

        MT3DMSData _mtData;

        MTPCBTN _pcBTN;

        public MTPCBTN PcBTN
        {
            get { return _pcBTN; }
            set { _pcBTN = value; }
        }

        #endregion

        Project project;
        #region  事件

        private void btnStrPer_Click(object sender, RoutedEventArgs e)
        {
            winMT3DStressPeriod winStressPeriod = new winMT3DStressPeriod(this._mtData.BasicTransport.StressPeriod);
            winStressPeriod.Owner = this;
            winStressPeriod.ResizeMode = ResizeMode.NoResize;
            winStressPeriod.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            winStressPeriod.ShowDialog();
        }

        private void btnOutCtrol_Click(object sender,RoutedEventArgs e)
        {
            winMT3DOutputControl winOutputControl = new winMT3DOutputControl(this._pcBTN.OutputControl);
            winOutputControl.Owner = this;
            winOutputControl.ResizeMode = ResizeMode.NoResize;
            winOutputControl.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            winOutputControl.ShowDialog();
            if (winOutputControl.DialogResult==true)
            {
                //this._pcBTN.SetOutputControl(winOutputControl.PcOutputControl);
                //this._pcBTN.OutputControl.Timesteps = winOutputControl.PcOutputControl.Timesteps;

                this._pcBTN.OutputControl = winOutputControl.PcOutputControl;

            }
        }
        MTPCPackages pcPages;
        private void btnPackage_Click(object sender, RoutedEventArgs e)
        {
            winMT3DPackage winPackage = new winMT3DPackage(pcPages);        
            winPackage.Owner = this;
            winPackage.ResizeMode = ResizeMode.NoResize;
            winPackage.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            if (winPackage.ShowDialog().Value)
            {
                pcPages = winPackage.PcPackages;
            }
        }

        private void btnDefSpe_Click(object sender, RoutedEventArgs e)
        {
            List<MTSpecies> species=new List<MTSpecies>();

            species.AddRange(this._pcBTN.Species);

            winDefineSpecies winDS = new winDefineSpecies(species);
            winDS.Grid = this._mtData.BasicTransport.Grid;
            winDS.Owner = this;
            winDS.ResizeMode = ResizeMode.NoResize;
            winDS.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            winDS.ShowDialog();
            if (winDS.DialogResult==true)
            {
               this._pcBTN.SetSpecies(winDS.ArrySpecies);
            }

        }

        private void btnUnit_Click(object sender, RoutedEventArgs e)
        {
            winMT3DUnits winUnit = new winMT3DUnits(this._mtData.BasicTransport);
            winUnit.Owner = this;
            winUnit.ResizeMode = ResizeMode.NoResize;
            winUnit.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            winUnit.ShowDialog();
        }

        private void btnHTOP_Click(object sender, RoutedEventArgs e)
        {
            //winGridHTOP winHTOP = new winGridHTOP(this._mtData.BasicTransport.Grid);
            //winHTOP.Owner = this;
            //winHTOP.ResizeMode = ResizeMode.NoResize;
            //winHTOP.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            //winHTOP.ShowDialog();


            MTGrid grid = this._mtData.BasicTransport.Grid;

            HA.ModFlow.Infrastructure.CellCenterDataSet3d ds = new Infrastructure.CellCenterDataSet3d(grid.Elevations,grid.GetICBUND(),grid.Nrows,grid.Ncols,grid.Nlayers);
            ds.Label = "HTOP";
            winCellCenterDSEdit win = new winCellCenterDSEdit(ds,project);
            win.Owner = this;
            win.Unit += "(" + HA.ModFlow.Domain.DomainLayer.UnitConverter.GetLength(_mtData.BasicTransport.ModelOption.Units.Length) + ")";
            win.WindowStartupLocation = WindowStartupLocation.CenterScreen;

            if (win.ShowDialog()==true)
            {
                
                grid.SetTopH(win.PcDs.Dataset.ToListData());
                if (project.CurrentDSData != null && project.CurrentDSData.Label == ds.Label)
                    project.OnCurrentDSChanged();
            }


        }

        private void btnICBUND_Click(object sender, RoutedEventArgs e)
        {
            //winGridICBUND winICBUND = new winGridICBUND(this._mtData.BasicTransport.Grid);
            //winICBUND.Owner = this;
            //winICBUND.ResizeMode = ResizeMode.NoResize;
            //winICBUND.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            //winICBUND.ShowDialog();





            MTGrid grid = this._mtData.BasicTransport.Grid;





            winCellCenterDSEdit win = new winCellCenterDSEdit(grid.GetICBoundDS(), project);
            win.Owner = this;
            win.Unit += "无量纲";
            win.WindowStartupLocation = WindowStartupLocation.CenterScreen;

            if (win.ShowDialog() == true)
            {

                grid.SetICBoundDS(win.PcDs.Dataset);
                if (project.CurrentDSData != null && project.CurrentDSData.Label == win.PcDs.Dataset.Label)
                    project.CurrentDSData = grid.GetICBoundDS();
            }

        }

        private void btnPorosity_Click(object sender, RoutedEventArgs e)
        {
            //winGridPorosity winPorosity = new winGridPorosity(this._mtData.BasicTransport.Grid);
            //winPorosity.Owner = this;
            //winPorosity.ResizeMode = ResizeMode.NoResize;
            //winPorosity.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            //winPorosity.ShowDialog();


            MTGrid grid = this._mtData.BasicTransport.Grid;

            HA.ModFlow.Infrastructure.CellCenterDataSet3d ds = new Infrastructure.CellCenterDataSet3d(grid.GetPorosity(), grid.GetICBUND(), grid.Nrows, grid.Ncols, grid.Nlayers);
            ds.Label = "Porosity";
            winCellCenterDSEdit win = new winCellCenterDSEdit(ds, project);          
            win.Owner = this;
            win.Unit += "无量纲";
            win.WindowStartupLocation = WindowStartupLocation.CenterScreen;

            if (win.ShowDialog() == true)
            {

                grid.SetPorosity(win.PcDs.Dataset.ToListData());
                if (project.CurrentDSData != null && project.CurrentDSData.Label == ds.Label)
                    project.OnCurrentDSChanged();
            }



        }

        private void btnConcentration_Click(object sender, RoutedEventArgs e)
        {
            int index = this.libSpecies.SelectedIndex;
            if (index >= 0)
            {
                int i=this._mtData.BasicTransport.Grid.Nrows;
                int j=this._mtData.BasicTransport.Grid.Ncols;
                int k=this._mtData.BasicTransport.Grid.Nlayers;

                HA.ModFlow.Infrastructure.CellCenterDataSet3d ds = new HA.ModFlow.Infrastructure.CellCenterDataSet3d(this._pcBTN.Species[index].InitConc, this._mtData.BasicTransport.Grid.GetICBUND(), i, j, k);
                ds.Label = this._pcBTN.Species[index].Name + "初始浓度";

                winCellCenterDSEdit win = new winCellCenterDSEdit(ds, project);

                win.Owner = this;
                win.ResizeMode = ResizeMode.NoResize;
                win.Unit += "(" + HA.ModFlow.Domain.DomainLayer.UnitConverter.GetConcentration(_mtData.BasicTransport.ModelOption.Units) + ")";
                win.WindowStartupLocation = WindowStartupLocation.CenterScreen;
                if (win.ShowDialog()==true)
                {

                    this._pcBTN.Species[index].SetInitConc(win.PcDs.Dataset.ToListData());
                    if (project.CurrentDSData != null && project.CurrentDSData.Label == ds.Label)
                        project.OnCurrentDSChanged();
                } 


               
            }
        }

        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            this._mtData.BasicTransport.Title1 = this.txtTitle1.Text;
            this._mtData.BasicTransport.Title2 = this.txtTitle2.Text;

            pcPages.ConformToDataCenter(this._mtData);
            this._pcBTN.ConformToDataCenter();
            this.DialogResult = true;
            this.Close();

        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            this.Close();
        }

        #endregion

     


     

      
       
}
}
